import BasePlugin from './BasePlugin';
import * as fabricModule from 'fabric';
const fabric = fabricModule.fabric || fabricModule;

/**
 * 文本插件 - 处理文本操作
 */
export default class TextPlugin extends BasePlugin {
    constructor() {
        super();
        this.textDefaults = {
            fontSize: 20,
            fontFamily: 'Arial',
            fill: '#000000',
            textAlign: 'left',
            lineHeight: 1.2,
            padding: 5,
            cornerColor: '#0099ff',
            cornerSize: 8,
            transparentCorners: false
        };
        this.isEditing = false;
        this.activeText = null;
        this.fonts = [
            'Arial',
            'Times New Roman',
            'Helvetica',
            'Courier New',
            'Georgia',
            'Verdana',
            'Impact'
        ];
    }

    /**
     * 初始化插件
     */
    init(editor) {
        super.init(editor);
        this._setupEventListeners();
        this._loadFonts();
    }

    /**
     * 设置文本默认样式
     */
    setTextDefaults(options = {}) {
        Object.assign(this.textDefaults, options);
    }

    /**
     * 添加文本
     */
    addText(text = '双击编辑文本', options = {}) {
        if (!this.isEnabled() || !this.canvas) return;

        const textObj = new fabric.IText(text, {
            ...this.textDefaults,
            ...options,
            left: options.left || this.canvas.width / 2,
            top: options.top || this.canvas.height / 2
        });

        this.canvas.add(textObj);
        this.canvas.setActiveObject(textObj);
        this._emitTextEvent('add', textObj);
        return textObj;
    }

    /**
     * 添加文本框
     */
    addTextbox(text = '双击编辑文本', options = {}) {
        if (!this.isEnabled() || !this.canvas) return;

        const textboxObj = new fabric.Textbox(text, {
            ...this.textDefaults,
            ...options,
            left: options.left || this.canvas.width / 2,
            top: options.top || this.canvas.height / 2,
            width: options.width || 200
        });

        this.canvas.add(textboxObj);
        this.canvas.setActiveObject(textboxObj);
        this._emitTextEvent('add', textboxObj);
        return textboxObj;
    }

    /**
     * 开始编辑文本
     */
    startEditing(textObj) {
        if (!this.isEnabled() || !this.canvas || !textObj || this.isEditing) return;

        if (textObj instanceof fabric.IText || textObj instanceof fabric.Textbox) {
            this.isEditing = true;
            this.activeText = textObj;
            textObj.enterEditing();
            this._emitTextEvent('editStart', textObj);
        }
    }

    /**
     * 结束编辑文本
     */
    endEditing() {
        if (!this.isEnabled() || !this.canvas || !this.isEditing || !this.activeText) return;

        this.activeText.exitEditing();
        this.isEditing = false;
        this._emitTextEvent('editEnd', this.activeText);
        this.activeText = null;
    }

    /**
     * 设置字体
     */
    setFont(textObj, fontFamily) {
        if (!this.isEnabled() || !this.canvas || !textObj) return;

        textObj.set('fontFamily', fontFamily);
        this.canvas.renderAll();
        this._emitTextEvent('fontChange', textObj);
    }

    /**
     * 设置字体大小
     */
    setFontSize(textObj, fontSize) {
        if (!this.isEnabled() || !this.canvas || !textObj) return;

        textObj.set('fontSize', fontSize);
        this.canvas.renderAll();
        this._emitTextEvent('fontSizeChange', textObj);
    }

    /**
     * 设置文本颜色
     */
    setColor(textObj, color) {
        if (!this.isEnabled() || !this.canvas || !textObj) return;

        textObj.set('fill', color);
        this.canvas.renderAll();
        this._emitTextEvent('colorChange', textObj);
    }

    /**
     * 设置文本对齐方式
     */
    setTextAlign(textObj, align) {
        if (!this.isEnabled() || !this.canvas || !textObj) return;

        textObj.set('textAlign', align);
        this.canvas.renderAll();
        this._emitTextEvent('alignChange', textObj);
    }

    /**
     * 设置行高
     */
    setLineHeight(textObj, lineHeight) {
        if (!this.isEnabled() || !this.canvas || !textObj) return;

        textObj.set('lineHeight', lineHeight);
        this.canvas.renderAll();
        this._emitTextEvent('lineHeightChange', textObj);
    }

    /**
     * 设置字体样式
     */
    setFontStyle(textObj, style) {
        if (!this.isEnabled() || !this.canvas || !textObj) return;

        switch (style) {
            case 'bold':
                textObj.set('fontWeight', textObj.fontWeight === 'bold' ? 'normal' : 'bold');
                break;
            case 'italic':
                textObj.set('fontStyle', textObj.fontStyle === 'italic' ? 'normal' : 'italic');
                break;
            case 'underline':
                textObj.set('underline', !textObj.underline);
                break;
            case 'linethrough':
                textObj.set('linethrough', !textObj.linethrough);
                break;
            case 'overline':
                textObj.set('overline', !textObj.overline);
                break;
        }

        this.canvas.renderAll();
        this._emitTextEvent('styleChange', textObj);
    }

    /**
     * 获取文本属性
     */
    getTextProperties(textObj) {
        if (!textObj) return null;

        return {
            text: textObj.text,
            fontFamily: textObj.fontFamily,
            fontSize: textObj.fontSize,
            fill: textObj.fill,
            textAlign: textObj.textAlign,
            lineHeight: textObj.lineHeight,
            fontWeight: textObj.fontWeight,
            fontStyle: textObj.fontStyle,
            underline: textObj.underline,
            linethrough: textObj.linethrough,
            overline: textObj.overline,
            left: textObj.left,
            top: textObj.top,
            width: textObj.width,
            angle: textObj.angle
        };
    }

    /**
     * 加载字体
     */
    _loadFonts() {
        // 这里可以实现动态加载字体的逻辑
        // 例如使用 WebFont Loader
    }

    /**
     * 发送文本事件
     */
    _emitTextEvent(type, textObj = null) {
        this.editor.emit('text:' + type, {
            type,
            target: textObj
        });
    }

    /**
     * 设置事件监听
     */
    _setupEventListeners() {
        if (!this.canvas) return;

        // 监听双击事件
        this.canvas.on('mouse:dblclick', (e) => {
            if (!this.isEnabled()) return;

            const target = e.target;
            if (target && (target instanceof fabric.IText || target instanceof fabric.Textbox)) {
                this.startEditing(target);
            }
        });

        // 监听文本编辑事件
        this.canvas.on('text:changed', (e) => {
            if (!this.isEnabled()) return;

            const textObj = e.target;
            this._emitTextEvent('change', textObj);
        });

        // 监听选择事件
        this.canvas.on('selection:created', (e) => {
            if (!this.isEnabled()) return;

            const target = e.target;
            if (target && (target instanceof fabric.IText || target instanceof fabric.Textbox)) {
                this._emitTextEvent('select', target);
            }
        });

        this.canvas.on('selection:cleared', () => {
            if (!this.isEnabled() || !this.isEditing) return;

            this.endEditing();
        });

        // 监听修改事件
        this.canvas.on('object:modified', (e) => {
            if (!this.isEnabled()) return;

            const target = e.target;
            if (target && (target instanceof fabric.IText || target instanceof fabric.Textbox)) {
                this._emitTextEvent('modify', target);
            }
        });
    }

    /**
     * 销毁插件
     */
    destroy() {
        if (this.canvas) {
            if (this.isEditing) {
                this.endEditing();
            }
            this.activeText = null;
        }
        super.destroy();
    }
} 